package pl.edu.mimuw.irs.core.model.design;

import java.io.Serializable;

import pl.edu.mimuw.irs.core.exception.InternalException;
import pl.edu.mimuw.irs.core.exception.InternalException.Cause;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-09
 */
public class Fragmentation implements Serializable {

	private static final long serialVersionUID = -8115350799516809295L;

	// *** Attributes
	
	private Type type;
	
	// Modifier is used to set the size of fragmentation - for example:
	// - 0 - no fragmentation - node has no other children
	// - 1 - simple fragmentation - in transition sequence it would mean T-P-T 
	// - 2 - double fragmentation - in transition sequence it would mean T-P-T-P-T 
	private int modifier;
	// Getters and setters
	public Type getType() {
		return type;
	}
	public void setType(Type type) {
		this.type = type;
	}
	public int getModifier() {
		return modifier;
	}
	public void setModifier(int modifier) {
		if (modifier < 0) 
			throw new InternalException(Cause.WORKFLOW_FRAGMENTATION_NEGATIVE_MODIFIER);
		this.modifier = modifier;
	}
	
	// *** Constructors
	
	public Fragmentation(Type type, int modifier) {
		setType(type);
		setModifier(modifier);
	}
	
	public Fragmentation(Type type) {
		this(type, 1);
	}
	
	public Fragmentation() {
		this(Type.NONE);
	}
	
	
	// *** Methods
	
	public Fragmentation copy() {
		return new Fragmentation(getType(), getModifier());
	}
	
	@Override
	public String toString() {
		return getType().toString().substring(0, 3) + "(" + getModifier() + ")";
	}
	
	// *** Enums
	
	/**
	 * Type of fragmentation describes what kind of split is used to build children of
	 * a node.
	 */
	public enum Type {
		SEQUENCE,
		CONCURRENCY,
		ALTERNATIVE,
		ITERATION,
		ACTIVITY,
		NONE;
	}
}
